cd "~/Dropbox/research/Denmark Housing Populism/replicationfiles/"

*REPLICATION FILES
*Article: Sheltering Populists?House Prices and the Support for Populist Parties
*Authors: Ben Ansell, Frederik Hjorth, Jacob Nyrup & Martin Vinæs Larsen
*Software: STATA 16.0
*Dependencies: none

*Replicated in this file: Tables 2, D2, L1.

*dataset used
use "data_dk_micro.dta", clear

*logfile
log using "log_dk_micro.txt", replace t

****************************************************************

**Tables and Figures in Main Text

*setting up controls
local zipcontrols="nwimm solo tenppl low_service low_manuf highlvl medianinc unemprate pop area"
local demographics="alder koen i.uddannelse i.civil i.indkomst "


*restricting sample
reg df c.hplevel c.renter `demographics' `zipcontrols' i.year,  vce(cluster zip)
keep if e(sample)==1

*estimating standard models
eststo a:  reg df hplevel  `zipcontrols' i.year,  vce(cluster zip)
reg df hplevel `demographics' `zipcontrols' i.year,  vce(cluster zip)
estadd local demo "$\checkmark$"
estimate store b
 reg df c.hplevel##c.renter `demographics' `zipcontrols' i.year,  vce(cluster zip)
margins, dydx(hplevel) over(renter) 
estadd local demo "$\checkmark$"
estimate store c



*writing table
foreach x in a b c{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
esttab a b c using table2.tex, keep(hplevel renter c.hplevel#c.renter) replace ///
nostar se nomtitles b(%9.1f) nogap ///
label stats(demo yearfe yearfe N rmse, fmt(%8.0f %8.0f %8.0f %8.0f %8.3f)   label("Demographic controls" "Zip-code controls" "Year FE" "Observations" "RMSE")) nonotes ///
title(Support for the Danish People's party: An Individual-level Approach} \footnotesize \label{indidka) ///
addnotes("Standard errors clustered on zip codes in parentheses." "Demographic controls: Age, gender, income, education and marital status." "Zip-code controls: See precinct-level analysis.")

****************************************************************

**Tables and Figures in Appendix

*Descriptive statistics (Appendix D)
file open anyname using "tabled2.txt", write text replace //using file write to create a descriptive table
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive Statistics, Denmark (individual) \label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  df hplevel blok renter holdning_indvandrere housingwealth    {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname


*More Individual-level Results (Appendix L)
eststo a: reg housingwealth hplevel  i.year `demographics' `zipcontrols',  vce(cluster zip)
eststo b: reg holdning_indvandrere hplevel  i.year `demographics' `zipcontrols',  vce(cluster zip)
eststo c: reg df c.hplevel##c.bloktidlvalg `demographics' `zipcontrols' i.year,  vce(cluster zip)

*writing table
foreach x in a b c{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
esttab a b c using tablel1.tex, keep(hplevel bloktidlvalg c.hplevel#c.bloktidlvalg ) replace ///
nostar se mtitles("Home Equity" "Anti-immigrant" "DPP support") b(%9.3f) nogap ///
label  stats(yearfe yearfe yearfe N rmse, fmt(%8.0f %8.0f %8.0f  %8.0f %8.3f)   label("Demographic controls" "Zip-code controls" "Year FE" "Observations" "RMSE"))  ///
title(Individual level results} \footnotesize \label{indidk) ///
addnotes("Coefficents show effects in percentage points. Clustered standard errors in parentheses. " "Demographic controls: Age, gender, income, education and marital status." "Zip-code controls: See precinct-level analysis.")

*closing log
log close

